<template>
  <div class="main_k">
    <div class="floating-particles" ref="particles"></div>
    <TransitionEffect :show="showTransition" />
    <div class="login-container">
      <h2 style="color:#00d9ff;">登录</h2>
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">用户名:</label>
          <input type="text" id="username" v-model="loginForm.username" required/>
        </div>
        <div class="form-group">
          <label for="password">密码:</label>
          <input type="password" id="password" v-model="loginForm.password" required/>
        </div>
        <button type="submit">登录</button>
        <h1 class="register" @click="navigateToRegister" style="cursor: pointer;">没有账号？立即注册</h1>
      </form>
    </div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus'
import { userApi } from '@/api'
import router from '@/router'
import TransitionEffect from '@/components/TransitionEffect.vue'

export default {
  name: 'Login',
  components: {
    TransitionEffect
  },
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      showTransition: false
    }
  },
  methods: {
    async handleLogin() {
      try {
        if (!this.loginForm.username || !this.loginForm.password) {
          ElMessage.warning('请输入用户名和密码');
          return;
        }

        console.log('开始登录请求，用户名:', this.loginForm.username);
        const response = await userApi.login(this.loginForm);
        console.log('登录响应:', response);
        
        if (response.code === 200 && response.token) {
          // 保存用户信息
          localStorage.setItem('jwtToken', response.token);
          localStorage.setItem('username', response.username);
          localStorage.setItem('userRole', response.role);
          
          ElMessage.success('登录成功');
          this.showTransition = true;
          
          // 3秒后跳转
          setTimeout(() => {
            this.showTransition = false;
            router.push('/blog')
              .then(() => {
                console.log('导航成功');
              })
              .catch(err => {
                console.error('导航错误:', err);
                if (err.name !== 'NavigationDuplicated') {
                  ElMessage.error('页面跳转失败，请手动刷新页面');
                }
              });
          }, 3000);
        } else {
          ElMessage.error(response.message || '登录失败');
          console.error('登录失败:', response);
        }
      } catch (error) {
        console.error('登录错误详情:', error);
        if (error.response?.data) {
          ElMessage.error(error.response.data.message || '登录失败');
        } else if (error.message === 'Network Error') {
          ElMessage.error('网络错误，请检查后端服务是否启动');
        } else {
          ElMessage.error('登录失败，请稍后重试');
        }
      }
    },
    navigateToRegister() {
      router.push('/register');
    },
    createParticles() {
      const particlesContainer = this.$refs.particles;
      if (!particlesContainer) return;
      
      for (let i = 0; i < 60; i++) {
        const particle = document.createElement('div');
        particle.className = 'particle-login';
        
        particle.style.left = `${Math.random() * 100}%`;
        particle.style.top = `${Math.random() * 100}%`;
        
        const size = Math.random() * 3 + 1;
        particle.style.width = `${size}px`;
        particle.style.height = `${size}px`;
        
        const delay = Math.random() * 5;
        const duration = Math.random() * 10 + 10;
        particle.style.animationDelay = `${delay}s`;
        particle.style.animationDuration = `${duration}s`;
        
        particlesContainer.appendChild(particle);
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.createParticles();
    });
  }
}
</script>

<style>
.main_k {
  background-image: url('../assets/myhome.png');
  background-size: cover;
  background-position: center;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
}

.main_k::before {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  background: radial-gradient(circle, rgba(12, 12, 50, 0.8) 0%, rgba(5, 5, 20, 0.95) 100%);
  top: 0;
  left: 0;
  z-index: 0;
}

.main_k::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: linear-gradient(rgba(0, 255, 255, 0.1) 1px, transparent 1px),
                    linear-gradient(90deg, rgba(0, 255, 255, 0.1) 1px, transparent 1px);
  background-size: 30px 30px;
  animation: gridMove 15s linear infinite;
  z-index: 1;
}

@keyframes gridMove {
  0% {
    background-position: 0 0;
  }
  100% {
    background-position: 30px 30px;
  }
}

.register {
  color: #00fdff;
  text-align: center;
  font-size: 19px;
  font-weight: 100;
  position: relative;
  bottom: -14px;
  cursor: pointer;
  text-shadow: 0 0 10px rgba(0, 253, 255, 0.7);
  transition: all 0.3s ease;
}

.register:hover {
  transform: scale(1.05);
  text-shadow: 0 0 15px rgba(0, 253, 255, 1);
}

.login-container {
  width: 350px;
  max-width: 95%;
  padding: 30px;
  border: 1px solid rgba(0, 217, 255, 0.3);
  border-radius: 10px;
  background: rgba(10, 20, 30, 0.7);
  backdrop-filter: blur(10px);
  box-shadow: 0 0 20px rgba(0, 217, 255, 0.3), 
              0 0 40px rgba(0, 217, 255, 0.1);
  position: relative;
  z-index: 10;
  animation: containerGlow 3s ease-in-out infinite alternate;
}

@keyframes containerGlow {
  from {
    box-shadow: 0 0 20px rgba(0, 217, 255, 0.3), 
                0 0 40px rgba(0, 217, 255, 0.1);
  }
  to {
    box-shadow: 0 0 25px rgba(0, 217, 255, 0.5), 
                0 0 50px rgba(0, 217, 255, 0.2);
  }
}

.login-container::before,
.login-container::after {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  width: calc(100% + 4px);
  height: calc(100% + 4px);
  border-radius: 12px;
  z-index: -1;
  animation: borderGlow 6s linear infinite;
  background: linear-gradient(45deg, 
    transparent, transparent, transparent,
    #00b3ff, #00b3ff, #00b3ff,
    transparent, transparent, transparent
  );
  background-size: 500%;
}

.login-container::after {
  filter: blur(18px);
  opacity: 0.5;
}

@keyframes borderGlow {
  0% { background-position: 0 0; }
  50% { background-position: 300% 0; }
  100% { background-position: 0 0; }
}

@media (max-width: 600px) {
  .login-container {
    max-width: 90%;
    padding: 20px;
  }
}

.form-group {
  margin-bottom: 20px;
  position: relative;
}

label {
  color: #0ff1de;
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 1px;
  text-shadow: 0 0 5px rgba(15, 241, 222, 0.5);
}

input[type="text"],
input[type="password"] {
  width: 100%;
  padding: 12px 15px;
  background: rgba(10, 14, 23, 0.7);
  border: 1px solid rgba(0, 253, 255, 0.3);
  border-radius: 4px;
  color: #fff;
  font-size: 16px;
  box-sizing: border-box;
  transition: all 0.3s ease;
  box-shadow: 0 0 5px rgba(0, 217, 255, 0.1) inset;
}

input[type="text"]:focus,
input[type="password"]:focus {
  border-color: rgba(0, 253, 255, 0.8);
  box-shadow: 0 0 10px rgba(0, 217, 255, 0.3);
  outline: none;
}

input[type="text"]:focus::after,
input[type="password"]:focus::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  box-shadow: 0 0 20px rgba(0, 217, 255, 0.8);
}

button {
  width: 100%;
  padding: 12px;
  background: linear-gradient(45deg, #0a84b3, #1ca2d3, #00d9ff);
  background-size: 200% 200%;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;
  font-weight: bold;
  letter-spacing: 1px;
  text-transform: uppercase;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(0, 217, 255, 0.3);
  position: relative;
  overflow: hidden;
  animation: gradientMove 3s ease infinite;
}

@keyframes gradientMove {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

button:hover {
  transform: translateY(-2px);
  box-shadow: 0 7px 20px rgba(0, 217, 255, 0.4);
}

button:active {
  transform: translateY(1px);
}

button::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  transform: translate(-50%, -50%) scale(0);
  opacity: 0;
  transition: transform 0.5s, opacity 0.5s;
}

button:active::after {
  transform: translate(-50%, -50%) scale(2);
  opacity: 0;
  transition: 0s;
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  font-size: 28px;
  letter-spacing: 2px;
  position: relative;
  text-shadow: 0 0 10px rgba(0, 217, 255, 0.5);
}

.floating-particles {
  position: absolute;
  width: 100%;
  height: 100%;
  z-index: 2;
  pointer-events: none;
}

.particle-login {
  position: absolute;
  width: 3px;
  height: 3px;
  background-color: rgba(0, 217, 255, 0.7);
  border-radius: 50%;
  animation: float-particle 3s linear infinite;
}

@keyframes float-particle {
  0% {
    transform: translateY(0) rotate(0deg);
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  100% {
    transform: translateY(-100px) rotate(360deg);
    opacity: 0;
  }
}
</style>